package com.hnyfkj.jyindustry.common;

import com.hnyfkj.jyindustry.biz.pi.entity.PiScreen;
import com.hnyfkj.jyindustry.biz.pi.service.PiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

@Component
public class SwingPi extends JFrame {

    public  MyJPanel mp = new  MyJPanel();
    public int index=0;
    public int startX = -1920;
    public int startY=0;
    public Timer timer;
    public static final ArrayList<ImageIcon> imgs = new ArrayList<>();


    public void swingpi(PiScreen pi_device, String action) {
        String[] split = pi_device.getPicUrl().split(",");
        if (pi_device.getDisplayType().equals("1")) {
            System.out.println("进来了,滑动显示图片");
            for (String s : split) {
                try {
                    URL url = new URL(s);
                    BufferedImage bufferedImage = ImageIO.read(url.openStream());
                    Image itemImage = bufferedImage.getScaledInstance(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.SCALE_SMOOTH);
                    ImageIcon imageIcon = new ImageIcon(itemImage);
                    imgs.add(imageIcon);
                } catch (IOException e) {
                    // TODO 自动生成的 catch 块
                    e.printStackTrace();
                }
            }
            mp = new MyJPanel();
            new JFrame();
            this.add(mp);
            //窗口最大化
            this.setExtendedState(JFrame.MAXIMIZED_BOTH);
            //设置关闭窗口就是关闭程序
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
            this.setTitle("窗口");
            this.setVisible(true);

            this.timer = new Timer(50, e -> {
                animate(action);
                mp.repaint();
            });
            timer.start();
        } else {
            System.out.println("进来了,进行直接显示图片");
            for (String s : split) {
                try {
                    URL url = new URL(s);
                    BufferedImage bufferedImage = ImageIO.read(url.openStream());
                    Image itemImage = bufferedImage.getScaledInstance(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.SCALE_SMOOTH);
                    ImageIcon imageIcon = new ImageIcon(itemImage);
                    imgs.add(imageIcon);
                } catch (IOException e) {
                    // TODO 自动生成的 catch 块
                    e.printStackTrace();
                }
            }
            startX = 0;
            mp = new MyJPanel();
            this.add(mp);
            this.setExtendedState(JFrame.MAXIMIZED_BOTH);
            //2.设置关闭窗口就是关闭程序
            //this.setDefaultCloseOperation(EXIT_ON_CLOSE);
            this.setTitle("窗口");
            this.setVisible(true);
            this.timer = new Timer(50, e -> {
                animate(action);
                mp.repaint();
            });
            timer.start();
        }

    }


    private void animate(String action) {
        int width = mp.getWidth();
        int iconWidth = imgs.get(index).getIconWidth();
        System.out.println("startx:" + startX + " , width: " + width + " , iconWidth: " + iconWidth);
//        if (startX == 0) {
//            this.timer.stop();
//            return;
//        }
        if (action.equals("0")) {
            this.timer.stop();
            return;
        }
        if (startX > (iconWidth + width)) {
            //  if (startX == 0) {
            startX = -1920;
        } else {
            startX += 4;
        }
    }

    class MyJPanel extends JPanel {

        @Override
        public synchronized void addComponentListener(ComponentListener l) {
            super.addComponentListener(l);
        }

        @Override
        public void paint(Graphics g) {
            System.out.println(startX);
            super.paint(g);
            //g.drawImage(imgs.get(index % imgs.size()).getImage(), startX, startY, this);
            for (int i = 0; i < imgs.size(); i++) {
                int width = 0;
                for (int j = 1; j <= i; j++) {
                    width += imgs.get(j).getIconWidth();
                }
                g.drawImage(imgs.get(i).getImage(), startX - width, startY, this);
            }
            //index++;
        }
    }

}